package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.*;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.bo.GoodsAllinone;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: mall38
 * @description:
 * @author: Wang
 * @create: 2022-05-05 20:31
 **/
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;

    /*
     * description 获取商品列表
     * @author Wangtianhao
     * @date 2022-05-09 20:01
     */
    @Override
    public GoodsDataVO query(String name, Integer goodsId, String goodsSn, BaseParamBO param) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<Goods> list = goodsMapper.select(name,goodsId,sort, order, goodsSn);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return GoodsDataVO.data(total, pages, limit, page, list);
    }

    /*
     * description 删除单个商品
     * @author Wangtianhao
     * @date 2022-05-09 20:02
     */
    @Override
    public void deleteGoodsById(Integer id) {
        goodsMapper.deleteGoodsById(id);
    }

    /*
     * description 获取品牌和类目列表
     * @author Wangtianhao
     * @date 2022-05-09 20:02
     */

    @Override
    public CatAndBrandDataVO queryCatAndBrand() {
        List<BrandBeanVO> list1 = goodsMapper.queryBrand();
        List<CategoryBeanVO> listFather = goodsMapper.queryFatherCategory();
        List<CategoryBeanVO> list2 = new ArrayList<>(listFather.size());

        for (CategoryBeanVO category : listFather) {
            List<CategoryBeanVO> children = goodsMapper.queryChildrenCategory(category.getValue());
            list2.add(new CategoryBeanVO(category.getValue(), category.getlabel(), children));
        }
        return CatAndBrandDataVO.data(list1, list2);
    }

    /*
     * description 获取商品评论列表
     * @author Wangtianhao
     * @date 2022-05-09 20:02
     */
    @Override
    public CommentDataVO queryComment(Integer userId, BaseParamBO param) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息

        List<Comment> list = goodsMapper.queryComment(sort, order, userId);
        List<CommentBeanVO> list2 = new ArrayList<>(list.size());

        PageInfo<Comment> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return CommentDataVO.data(total, pages, limit, page, list);
    }

    /*
     * description 删除单个评论
     * @author Wangtianhao
     * @date 2022-05-09 20:03
     */

    @Override
    public void deleteCommentById(Integer id) {
        goodsMapper.deleteCommentById(id);
    }

    /*
     * description 新增商品
     * @author Wangtianhao
     * @date 2022-05-09 20:03
     */
    @Override
    public void createGoodsByGoods(GoodsAllinone allioneGoods) {

        goodsMapper.insertSelective(allioneGoods.getGoods());
        Integer goodId = allioneGoods.getGoods().getId();
        goodsMapper.setRetailPrice(goodId,allioneGoods.getProducts().get(0).getPrice());

        List<GoodsAttribute> attributesList = allioneGoods.getAttributes();
        for (GoodsAttribute attribute : attributesList) {
            attribute.setGoodsId(goodId);
            goodsAttributeMapper.insertSelective(attribute);
        }

        List<GoodsSpecification> specificationsList = allioneGoods.getSpecifications();
        for (GoodsSpecification specification : specificationsList) {
            specification.setGoodsId(goodId);
            goodsSpecificationMapper.insertSelective(specification);
        }

        List<GoodsProduct> productsList = allioneGoods.getProducts();
        for (GoodsProduct product : productsList) {
            product.setGoodsId(goodId);
            goodsProductMapper.insertSelective(product);
            if(product.getPrice()==null){
                product.setGoodsId(goodId);
                goodsProductMapper.insertSelective(product);
            }
        }
    }

    /*
     * description 获取商品信息
     * @author Wangtianhao
     * @date 2022-05-09 20:03
     */

    @Override
    public GoodsDetailVO getGoodsDetail(Integer id) {
        List<Integer> categoryIdsList = new ArrayList<>();
        Integer categoryId1 = goodsMapper.selectCategoryId1(id);
        Integer categoryId2 = goodsMapper.selectCategoryId2(id);
        categoryIdsList.add(categoryId1);
        categoryIdsList.add(categoryId2);

        Goods goods = goodsMapper.selectByPrimaryKey(id);

        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria = goodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<GoodsAttribute> attributesList = goodsAttributeMapper.selectByExample(goodsAttributeExample);

        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria1 = goodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<GoodsSpecification> specificationsList = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);

        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria criteria2 = goodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<GoodsProduct> productsList = goodsProductMapper.selectByExample(goodsProductExample);

        return GoodsDetailVO.data(categoryIdsList, goods, specificationsList, productsList, attributesList);
    }

    /*
     * description 更新商品
     * @author Wangtianhao
     * @date 2022-05-09 20:03
     */
    @Override
    public void updateGoodsByGoods(GoodsAllinone allioneGoods) {

        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();

        criteria.andIdEqualTo(allioneGoods.getGoods().getId());
        criteria.andDeletedEqualTo(false);

        Goods goods = allioneGoods.getGoods();
        goods.setId(null);
        goodsMapper.updateByExampleSelective(goods,goodsExample);

        Integer goodId =(Integer.valueOf(allioneGoods.getGoods().getGoodsSn()));

        List<GoodsAttribute> attributesList = allioneGoods.getAttributes();
        for (GoodsAttribute attribute : attributesList) {
            Integer goodsId = goodsMapper.selectGoodsIdByName(allioneGoods.getGoods().getName());
            if (attribute.getId()==null) {
                goodsAttributeMapper.insertAttribute(allioneGoods,goodsId,attribute);
            } else {
                if (attribute.getDeleted() == true) {
                    goodsAttributeMapper.updateAttributeDeleted(attribute);
                }
            }
        }
        List<GoodsSpecification> specificationsList = allioneGoods.getSpecifications();
        for (GoodsSpecification specification : specificationsList) {
            goodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        List<GoodsProduct> productsList = allioneGoods.getProducts();
        for (GoodsProduct product : productsList) {
            Integer goodsId = goodsMapper.selectGoodsIdByName(allioneGoods.getGoods().getName());
            goodsProductMapper.setProduct(product);
            goodsMapper.setRetailPrice(goodsId,product.getPrice());
        }
    }

}
